package cn.iocoder.yudao.module.happycore.service.oxmodel;

import cn.iocoder.yudao.module.happycore.constants.ModeStatusEnum;
import cn.iocoder.yudao.module.happycore.controller.admin.oxmodel.vo.OxModelPageReqVO;
import cn.iocoder.yudao.module.happycore.controller.admin.oxmodel.vo.OxModelSaveReqVO;
import cn.iocoder.yudao.module.happycore.controller.admin.oxproject.vo.TrainResponse;
import cn.iocoder.yudao.module.happycore.dal.dataobject.oxmodel.OxModelDO;
import cn.iocoder.yudao.module.happycore.dal.dataobject.oxproject.OxProjectDO;
import cn.iocoder.yudao.module.happycore.dal.mysql.oxmodel.OxModelMapper;
import cn.iocoder.yudao.module.happycore.dal.mysql.oxproject.OxProjectMapper;
import cn.iocoder.yudao.module.happycore.service.PythonPredictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;



import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.module.happycore.constants.ErrorCodeConstants.OX_MODE_NOT_EXISTS;
import static cn.iocoder.yudao.module.happycore.constants.ErrorCodeConstants.OX_MODE_NOT_TRAINED;


/**
 * 模型 Service 实现类
 *
 * @author 昂热
 */
@Service
@Validated
public class OxModelServiceImpl implements OxModelService {

    @Resource
    private OxModelMapper oxModelMapper;
    @Autowired
    private PythonPredictService pythonPredictService;
    @Autowired
    private OxProjectMapper oxProjectMapper;

    @Override
    public Long createOxMode(OxModelSaveReqVO createReqVO) {
        // 插入
        OxModelDO oxMode = BeanUtils.toBean(createReqVO, OxModelDO.class);
        oxModelMapper.insert(oxMode);

        // 返回
        return oxMode.getId();
    }

    @Override
    public void updateOxMode(OxModelSaveReqVO updateReqVO) {
        // 校验存在
        validateOxModeExists(updateReqVO.getId());
        // 更新
        OxModelDO updateObj = BeanUtils.toBean(updateReqVO, OxModelDO.class);
        oxModelMapper.updateById(updateObj);
    }

    @Override
    public void deleteOxMode(Long id) {
        // 校验存在
        validateOxModeExists(id);

        // 删除
        oxModelMapper.deleteById(id);
    }

    @Override
        public void deleteOxModeListByIds(List<Long> ids) {
        // 删除
        oxModelMapper.deleteByIds(ids);
        }


    private void validateOxModeExists(Long id) {
        if (oxModelMapper.selectById(id) == null) {
            throw exception(OX_MODE_NOT_EXISTS);
        }
    }

    @Override
    public OxModelDO getOxMode(Long id) {
        return oxModelMapper.selectById(id);
    }

    @Override
    public PageResult<OxModelDO> getOxModePage(OxModelPageReqVO pageReqVO) {
        return oxModelMapper.selectPage(pageReqVO);
    }

    @Override
    public void trainDoneCallBack(TrainResponse trainResponse) {
        validateOxModeExists(trainResponse.getModeId());
        OxModelDO oxModelDO = oxModelMapper.selectById(trainResponse.getModeId());
        oxModelDO.setAccuracy(trainResponse.getAccuracy());
        oxModelDO.setStatus(ModeStatusEnum.DONE.getStatus());
        oxModelDO.setModePath(trainResponse.getModelPath());
        oxModelMapper.updateById(oxModelDO);

    }

    @Override
    public String predictDogBreed(String imagePath, Long modeId) {
        validateOxModeExists(modeId);
        OxModelDO oxModelDO = oxModelMapper.selectById(modeId);
        if (!ModeStatusEnum.DONE.getStatus().equals(oxModelDO.getStatus())) {
            throw exception(OX_MODE_NOT_TRAINED);
        }
        Long projectId = oxModelDO.getProjectId();
        OxProjectDO oxProjectDO = oxProjectMapper.selectById(projectId);

        return pythonPredictService.predictDogBreed(oxModelDO.getModePath(), oxProjectDO.getCategoryList(),imagePath ).getPredicted_breed();
    }


}